/**
 * Created with JetBrains PhpStorm.
 * User: Artem
 * Date: 19.03.13
 * Time: 12:52
 * To change this template use File | Settings | File Templates.
 */

Ext.define("Pro.controller.UpdRemEmployee", {
    extend: "Ext.app.Controller",

    views: [
        "UpdateOrRemoveEmployee"
    ],
    stores: [
        "Teams"
    ],

    refs: [
        {
            ref: "emplPanel",
            selector: "employeespanel"
        },
        {
            ref: "updOrRemFormPanel",
            selector: "updateorremoveemployee #updateOrRemoveFormPanel"
        },
        {
            ref: "removeOrUpdatePanel",
            selector: "updateorremoveemployee"
        },
        {
            ref: "requestTab",
            selector: "consolepanel #request"
        },
        {
            ref: "respondTab",
            selector: "consolepanel #respond"
        },
        {
            ref: "eventLogArea",
            selector: "consolepanel #eventLogArea"
        }
    ],

    init: function(){
        this.control({
            "updateorremoveemployee #update": {
                click: this.onUpdateClick
            },
            "updateorremoveemployee #remove": {
                click: this.onRemoveClick
            }
        });
    },

    onUpdateClick: function(){
        var updOrRemFormPanel = this.getUpdOrRemFormPanel();
        var thisController = this;
        updOrRemFormPanel.getForm().submit({
            url: "globallogic/update",
            success: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_SUCCESS,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            },
            failure: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_FAILURE,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            }
        });
        this.getRemoveOrUpdatePanel().close();
    },

    onRemoveClick: function(){
        var updOrRemFormPanel = this.getUpdOrRemFormPanel();
        var thisController = this;
        updOrRemFormPanel.getForm().submit({
            url: "globallogic/delete",
            success: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_SUCCESS,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            },
            failure: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_FAILURE,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            }
        });
        this.getRemoveOrUpdatePanel().close();
    },

    printRespond: function(action){
        var respondTab = this.getRespondTab();
        var eventLogArea = this.getEventLogArea();
        eventLogArea.setValue(eventLogArea.getValue() + (new Date()).toLocaleString() + " " + action.result.message + "\n");
        respondTab.setValue(action.response.responseText);
    },

    printRequest: function(action){
        var requestTab = this.getRequestTab();
        var req = "";
        req += ("url:\t\t\t" + action.response.request.options.url + "\n");
        req += ("method:\t\t" + action.response.request.options.method + "\n");
        req += "params: \n"
        req += ("\tid:\t\t" + action.response.request.options.params.id + "\n");
        req += ("\tteam:\t" + action.response.request.options.params.team + "\n");
        req += ("\tname:\t" + action.response.request.options.params.name + "\n");
        req += ("\tage:\t\t" + action.response.request.options.params.age + "\n");
        req += ("\tphone:\t" + action.response.request.options.params.phone + "\n");
        req += ("\tmarried:\t" + action.response.request.options.params.married + "\n");
        requestTab.setValue(req);
    }
});